<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>运单费用 - TMS</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <link rel="stylesheet" href="/view/tms/static/css/iframe.css?v=1.0">
    <style>
        .layui-form-item {
            margin: 0;
        }
        
        .layui-form-item .layui-inline {
            margin-bottom: 0;
            margin-right: 6px;
        }
        
        .layui-form-item .layui-inline .layui-input-inline {
            margin-right: 4px;
            width: 160px;
        }
        
        .layui-form-item .layui-inline .layui-form-mid {
            margin-right: 4px;
        }
        
        .fold {
            font-size: 23px;
            font-style: normal;
            font-weight: bold;
            cursor: pointer;
        }
        
        .sub-table .layui-table-view {
            margin: 3px;
            margin-left: -1px;
        }
        
        .sub-tool-bar {
            margin: 0 30px 5px 0;
            text-align: left;
            line-height: 30px;
        }
        
        .sub-tool-bar a {
            margin: 0 10px;
            color: #055bae;
            text-decoration: underline;
        }
        
        .time-btn-sel {
            background: #cecece;
            color: #000;
        }
        
        searchMore {
            display: none;
        }
    </style>
</head>

<body>
    <!--start: 搜索-->
    <div class="panel-handle search-bar">
        <form class="layui-form layui-form-pane" autocomplete="off">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:90px;">运输企业</label>
                    <div class="layui-input-inline">
                        <input type="text" name="transportCompany" placeholder="运输企业" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:90px;">做柜时间</label>
                    <div class="layui-input-inline">
                        <input type="text" name="doarkDateStart" id="doarkDateStart" readonly placeholder="起始时间" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline">
                        <input type="text" name="doarkDateEnd" id="doarkDateEnd" readonly placeholder="截止时间" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                <option value="0">未审核</option>
                <option value="1">已审核</option>
              </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layui-btn-danger" lay-submit lay-filter="search"><i class="layui-icon">&#xe615;</i>查询</button>
                    <button class="layui-btn layui-btn-primary" type="reset" style="margin:0;">重置</button>
                </div>
            </div>
            <div class="layui-form-item" style="margin-top:10px;">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:90px;">S/O No.</label>
                    <div class="layui-input-inline">
                        <input type="text" name="soNumber" placeholder="S/O号码" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:90px;">柜号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="arkNo" placeholder="柜号" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label" style="width:90px;">订单号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderNo" placeholder="订单号" class="layui-input">
                    </div>
                </div>
            </div>
        </form>
        <div class="clr"></div>
    </div>
    <!--end: 搜索-->

    <!--start: 按钮组-->
    <div class="panel-handle btns-bar">
        <div class="layui-btn-group fl">
            <button class="layui-btn layui-btn-normal" data-type="affirmFee">确认费用</button>
            <button class="layui-btn layui-btn-normal" data-type="dissent">费用异议</button>
            <button class="layui-btn layui-btn-normal" data-type="approve">审核通过</button>
            <button class="layui-btn layui-btn-normal" data-type="approveNot">审核不通过</button>
        </div>
        <div class="clr"></div>
    </div>
    <!--end: 按钮组-->

    <!--start:表格列表-->
    <table class="layui-hide" id="tableList" lay-filter="tbar"></table>
    <!--end:表格列表-->

    <script type="text/html" id="statusTpl">
        {{setCostStatus(d.receivableCostStatus)}}
    </script>
    <script type="text/html" id="doarkDateTpl">
        {{ new Date(d.doarkDate).format('MM-dd hh:mm') }}
    </script>
    <script type="text/html" id="arkTypeTpl">
        {{# if(d.arkType != null){ }} {{# if(d.arkType.indexOf('RF') > -1){ }}
        <span style="color:#34a8ff;">{{d.arkType}}</span> {{# }else{ }} {{d.arkType}} {{# } }} {{# } }}
    </script>
    <script type="text/html" id="addressTpl">
        {{# var $newArr = d.address.split(';') }} {{# if($newArr.length > 0){ }} {{# $newArr = deWeightArr($newArr) }} {{$newArr.join(' || ')}} {{# } }}
    </script>
    <script type="text/html" id="receiveFreightTpl">
        {{# if(d.receiveFreight != null){ }} {{d.receiveFreight}} {{# }else{ }}0{{# } }}
    </script>
    <script type="text/html" id="receiveExCostTpl">
        {{# if(d.receiveExCost != null){ }}
        <a lay-event="receiveExCost" style="color:#f00;">{{d.receiveExCost}}</a> {{# }else{ }}0{{# } }}
    </script>
</body>
<script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/layui/layui.js"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>

<script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
<script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
<script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
<script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>

<script>
    layui.use(['form', 'layer', 'table', 'tms_tab', 'laydate'], function() {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate,
            tmsTab = layui.tms_tab,
            $ = layui.jquery;

        //初始化日期时间
        laydate.render({
            elem: '#doarkDateStart'
        });
        laydate.render({
            elem: '#doarkDateEnd'
        });

        //初始化表格数据
        setTableData({});

        //监听提交
        form.on('submit(search)', function(data) {
            var v1 = data.field.doarkDateStart,
                v2 = data.field.doarkDateEnd;
            if (v1.length > 0 && v2.length == 0) {
                layer.msg('请选择截止时间');
                return false;
            }
            if (v1.length == 0 && v2.length > 0) {
                layer.msg('请选择起始时间');
                return false;
            }
            if (v1.length > 0 && v2.length > 0) {
                v1 = new Date(v1);
                v2 = new Date(v2);
                if (v1 > v2) {
                    layer.msg('截止时间要大于起始时间');
                    return false;
                }
            }

            var $saveData = data.field;
            setTableData($saveData);
            return false;
        });

        //方法级渲染
        function setTableData($searchData) {
            table.render({
                elem: '#tableList',
                url: '/ucenter/tms/waybill/waybill/searchCheckList.shtml',
                method: 'post',
                where: $searchData,
                response: {
                    statusCode: 'SUCCESS',
                    dataName: 'objects'
                },
                cols: [
                    [{
                        checkbox: true,
                        fixed: true
                    }, {
                        field: 'id',
                        title: '序号',
                        width: 80,
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'receivableCostStatus',
                        title: '状态',
                        width: 100,
                        sort: true,
                        align: 'center',
                        templet: '#statusTpl'
                    }, {
                        field: 'transportCompany',
                        title: '运输企业',
                        width: 100,
                        align: 'center'
                    }, {
                        field: 'orderNo',
                        title: '订单号码',
                        width: 150,
                        align: 'center'
                    }, {
                        field: 'soNumber',
                        title: 'S/O',
                        width: 150,
                        align: 'center'
                    }, {
                        field: 'arkNo',
                        title: '柜号',
                        width: 150,
                        align: 'center'
                    }, {
                        field: 'doarkDate',
                        title: '做柜时间',
                        width: 150,
                        sort: true,
                        align: 'center',
                        templet: '#doarkDateTpl'
                    }, {
                        field: 'arkType',
                        title: '箱型',
                        width: 100,
                        align: 'center',
                        templet: '#arkTypeTpl'
                    }, {
                        field: 'carNo',
                        title: '车牌',
                        width: 100,
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'receiveFreight',
                        title: '运费',
                        width: 100,
                        align: 'center',
                        templet: '#receiveFreightTpl'
                    }, {
                        field: 'receiveExCost',
                        title: '异常费用',
                        width: 100,
                        align: 'center',
                        templet: '#receiveExCostTpl'
                    }, {
                        field: 'recostRemark',
                        title: '备注',
                        width: 140,
                        sort: true,
                        align: 'center'
                    }, {
                        field: 'confirmMan',
                        title: '确认人',
                        width: 100,
                        align: 'center'
                    }, {
                        field: 'address',
                        title: '装卸地点',
                        width: 100,
                        align: 'center',
                        templet: '#addressTpl'
                    }]
                ],
                id: 'dataReload',
                page: false,
                height: 'full-178',
                size: 'sm',
                even: true
            });

            //添加快捷搜索
            var $shortBarHtml = $('#shortBar').html();
            $('.layui-table-tool').prepend('<div class="short-bar">' + $shortBarHtml + '</div>');
        }

        //监听工具条
        table.on('tool(tbar)', function(obj) {
            var data = obj.data;
            //费用异议详情
            if (obj.event === 'receiveExCost') {
                var $elem = this;
                $.get('/ucenter/tms/waybill/waybill/searchExDetail.shtml', {
                    waybillId: data.id
                }, function(d) {
                    var $code = d.code,
                        $msg = d.msg,
                        $objects = d.objects;

                    if ($code === 'SUCCESS') {
                        if ($objects.length > 0) {
                            var $trHtml = '',
                                $trRows = $objects.length,
                                $totalFee = 0,
                                $remark = 'test',
                                $dissentRemark = 'test';
                            for (var $i = 0; $i < $objects.length; $i++) {
                                if ($i == 0) {
                                    $trHtml = '<tr><td rowspan="' + $trRows + '" align="center">运输异常费用</td><td align="center">' + $objects[$i].recostName + '</td><td>' + $objects[$i].recostAmount + '</td></tr>'
                                } else {
                                    $trHtml = '<tr><td align="center">' + $objects[$i].recostName + '</td><td>' + $objects[$i].recostAmount + '</td></tr>'
                                }
                                $totalFee += $objects[$i].recostAmount;
                            }

                            var $html = '<style>' +
                                '.cost-table {border-collapse:collapse; color:#000; margin:7px 0; width:100%;}' +
                                '.cost-table th, .cost-table td {border:#bbb 1px solid; padding:5px; background:#fff;}' +
                                '</style>' +
                                '<table class="cost-table">' +
                                '<colgroup><col width="35%"><col width="25%"><col width="40%"></colgroup>' +
                                '<thead><tr><th colspan="3">异常费用明细</th></tr></thead><tbody>' + $trHtml +
                                '<tr><td colspan="2" align="center">异常费用合计</td><td>' + $totalFee + '</td></tr>' +
                                '<tr><th colspan="3">费用备注</th></tr>' +
                                '<tr><td colspan="3">' + $remark + '</td></tr>' +
                                '<tr><th colspan="3">异议原因</th></tr>' +
                                '<tr><td colspan="3">' + $dissentRemark + '</td></tr>' +
                                '</tbody></table>';

                            layer.tips($html, $elem, {
                                tips: [3, '#eee'],
                                time: 1000 * 60 * 5,
                                shadeClose: true,
                                shade: 0.01,
                                area: '280px'
                            });
                        }
                    }
                    return false;
                }, 'JSON');
            }
        });

        var active = {
            //确认费用
            affirmFee: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else {
                    var $waybillIds = [],
                        $isYes = [];
                    for (var $i = 0; $i < data.length; $i++) {
                        if (data[$i].receivableCostStatus == 2) {
                            $waybillIds.push(data[$i].id);
                        } else {
                            $isYes.push(1);
                        }
                    }

                    if ($waybillIds.length == 0) {
                        parent.layer.msg('没有可以确认费用的的运单！');
                        return false;
                    }
                    if ($isYes.length > 0) {
                        parent.layer.msg('只有已核算的运单才能确认费用！');
                        setTimeout(function() {
                            confirmFee($waybillIds);
                        }, 3000);
                    } else {
                        confirmFee($waybillIds);
                    }

                    //接口
                    function confirmFee(ids) {
                        parent.layer.confirm('确定要确认费用吗？', function(index) {
                            var $saveData = {
                                ids: ids
                            };
                            $.ajax({
                                type: 'POST',
                                url: '/ucenter/tms/waybill/waybill/confirmCost.shtml',
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify($saveData),
                                success: function(d) {
                                    var $code = d.code,
                                        $msg = d.msg,
                                        $objects = d.objects;

                                    if ($code === 'SUCCESS') {
                                        window.location.reload();
                                    } else {
                                        parent.layer.msg('操作失败！');
                                    }
                                }
                            });
                            parent.layer.close(index);
                            return false;
                        });
                    }
                }
            },
            //费用异议
            dissent: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else if (data.length > 1) {
                    parent.layer.alert('只能选择一条数据！');
                } else {
                    if (data[0].receivableCostStatus != 2) {
                        parent.layer.msg('当前状态无法填写费用异议！');
                        return false;
                    }

                    layer.prompt({
                        title: '费用异议（' + data[0].id + '）',
                        formType: 2
                    }, function(text, index) {
                        layer.close(index);
                        var $saveData = {
                            ids: [data[0].id],
                            costObjection: text
                        }
                        $.ajax({
                            type: 'POST',
                            url: '/ucenter/tms/waybill/waybill/costObjection.shtml',
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify($saveData),
                            success: function(d) {
                                var $code = d.code,
                                    $msg = d.msg,
                                    $objects = d.objects;

                                if ($code === 'SUCCESS') {
                                    window.location.reload();
                                } else {
                                    parent.layer.msg('操作失败！');
                                }
                            }
                        });
                        return false;
                    });
                }
            },
            //审核通过
            approve: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else {
                    var $waybillIds = [],
                        $isYes = [];
                    for (var $i = 0; $i < data.length; $i++) {
                        if (data[$i].receivableCostStatus == 2 || data[$i].receivableCostStatus == 3) {
                            $waybillIds.push(data[$i].id);
                        } else {
                            $isYes.push(1);
                        }
                    }

                    //角色（跟单：monoApproved.shtml，财务：financeApproved.shtml）
                    var $userRole = 0,
                        $url = '';
                    if ($userRole == 0) {
                        $url = 'monoApproved';
                    } else if ($userRole == 1) {
                        $url = 'financeApproved';
                    }

                    if ($waybillIds.length == 0) {
                        parent.layer.msg('没有可以审核通过的的运单！');
                        return false;
                    }
                    if ($isYes.length > 0) {
                        parent.layer.msg('只有已核算和已确认的运单才能审核通过！');
                        setTimeout(function() {
                            approveFee($waybillIds, $url);
                        }, 3000);
                    } else {
                        approveFee($waybillIds, $url);
                    }

                    //接口
                    function approveFee(ids, url) {
                        parent.layer.confirm('确定审核通过吗？', function(index) {
                            var $saveData = {
                                ids: ids
                            };
                            $.ajax({
                                type: 'POST',
                                url: '/ucenter/tms/waybill/waybill/' + url + '.shtml',
                                dataType: "json",
                                contentType: "application/json",
                                data: JSON.stringify($saveData),
                                success: function(d) {
                                    var $code = d.code,
                                        $msg = d.msg,
                                        $objects = d.objects;

                                    if ($code === 'SUCCESS') {
                                        window.location.reload();
                                    } else if ($code === 'WARNING_RECEIVECOST_NOTCONFIRM') {
                                        parent.layer.msg($msg);
                                    } else {
                                        parent.layer.msg('操作失败！');
                                    }
                                }
                            });
                            parent.layer.close(index);
                            return false;
                        });
                    }
                }
            },
            //审核不通过
            approveNot: function() {
                var checkStatus = table.checkStatus('dataReload'),
                    data = checkStatus.data;
                if (data.length == 0) {
                    parent.layer.alert('你还未选中数据！');
                } else if (data.length > 1) {
                    parent.layer.alert('只能选择一条数据！');
                } else {
                    if (data[0].receivableCostStatus != 2 || data[0].receivableCostStatus != 3) {
                        return false;
                    }

                    layer.prompt({
                        title: '审核不通过（' + data[0].id + '）',
                        formType: 2
                    }, function(text, index) {
                        layer.close(index);
                        var $saveData = {
                            ids: [data[0].id],
                            costObjection: text
                        }
                        $.ajax({
                            type: 'POST',
                            url: '/ucenter/tms/waybill/waybill/nonApproved.shtml',
                            dataType: "json",
                            contentType: "application/json",
                            data: JSON.stringify($saveData),
                            success: function(d) {
                                var $code = d.code,
                                    $msg = d.msg,
                                    $objects = d.objects;

                                if ($code === 'SUCCESS') {
                                    window.location.reload();
                                } else if ($code === 'WARNING_RECEIVECOST_NOTCONFIRM') {
                                    parent.layer.msg($msg);
                                } else {
                                    parent.layer.msg('操作失败！');
                                }
                            }
                        });
                        return false;
                    });
                }
            }
        };

        $('.btns-bar .layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>

</html>